package clientGUI.studentGUI;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.*;

import clientApp.Plan;
import clientApp.ProjectConstants;
import clientApp.Semester;
import clientApp.VirtualAdvisor;
import clientGUI.PassBackToable;

public class SelectCoursesGUI_Plan {
	private VirtualAdvisor myVA;
	private Plan plan;
	private PassBackToable parent;
	private JFrame frame;
	private Semester presentSemester = null;
	private String cmd;
	public SelectCoursesGUI_Plan(PassBackToable parent, String cmd, Plan plan, VirtualAdvisor myVA)
	{
		this.plan = plan;
		this.parent = parent;
		this.myVA = myVA;
		this.cmd = cmd;
		frame = GUI_Function_Library.displayPane("Edit/Remove Courses", mainPane(), false);
	}
	
	private int countChecks(LinkedList<JCheckBox> checkList)
	{
		int checkCount = 0;
		for(JCheckBox checkBx: checkList)
			if(checkBx.isSelected())
				checkCount ++;
		return checkCount;
	}
	private JPanel mainPane()
	{
		final JPanel		dispClassesPane = new JPanel();
		dispClassesPane.setLayout(new BoxLayout(dispClassesPane, BoxLayout.PAGE_AXIS));
		
		JLabel semLabel = new JLabel("Select Semester");
		
		String[] eligibleSemesters = EditSemestersInPlan.getSemesterSeasonsAndYears(plan);
		if (eligibleSemesters == null) {
			//TODO: once add/remove semesters are implemented, this can be handled better
			eligibleSemesters = new String[]{""};
		}
		final JComboBox	semDropdown = new JComboBox(eligibleSemesters);
		
		final LinkedList<JCheckBox> courseChecks = new LinkedList<JCheckBox>();
		semDropdown.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				courseChecks.clear();
				
				String[] semString = ((String)semDropdown.getSelectedItem()).split(" ");
				int seasonInt = ProjectConstants.getSeasonInt(semString[0]);
				Semester semesterChosen = plan.getSemester(seasonInt, Integer.parseInt(semString[1]));
				System.out.println("SelectCoursesGUI_Plan:Num Semester Courses:" + semesterChosen.getCourseIDs().size());
				presentSemester = semesterChosen;
				for(String courseID: semesterChosen.getCourseIDs())
				{
					
					//TODO: remove previous classes
					JCheckBox courseCheckBox = new JCheckBox(courseID);
					dispClassesPane.add(courseCheckBox);
					
					courseChecks.add(courseCheckBox);
				}
				frame.validate();
			}
		});
		
		JButton editCourseButton = new JButton("Edit Course Semester");
		JButton removeCoursesButton = new JButton("Remove Courses");
		editCourseButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				
				try{
					if(countChecks(courseChecks) != 1)
						throw new Exception("1 and only 1 course selection please");
					String chosenCourse = courseChecks.getFirst().getText();
					System.out.println("SelectCoursesGUI_Plan:Chosen Course!!!!!!!: " + chosenCourse);
//					JOptionPane.showMessageDialog(null, "Needs Functionality");
					
					
					ArrayList<String> semesterAL = plan.getAllSemesterSeasonSpaceYear();
					String[] semesterArr = new String[semesterAL.size()];
					for(int i = 0; i < semesterAL.size(); i++)
						semesterArr[i] = semesterAL.get(i);
					
					String instruction = "You have chosen to move course " + chosenCourse + "to another semester.";
					String nextSemester = (String)JOptionPane.showInputDialog(null, instruction, "Select semester.", JOptionPane.PLAIN_MESSAGE, null, semesterArr, semesterArr[0]);
					
					String[] split = nextSemester.split(" ");
					int season = ProjectConstants.getSeasonInt(split[0]);
					int year = Integer.parseInt(split[1]);
					
					parent.passBack(cmd, presentSemester.getSeason() + "," + presentSemester.getYear() + " " + chosenCourse + " " + season + "," + year);
					
					JOptionPane.showMessageDialog(null, "NEEDS TO ACTUALLY REMOVE THE COURSE");
				}catch(Exception e1)
				{
					GUI_Function_Library.exceptionHandler("SelectCoursesGUI_Plan: ", e1);
				}
			}
		});
		removeCoursesButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(null, "ADD FUNCTIONALITY");
			}
		});
		JPanel buttonPane = new JPanel(new GridLayout(1,2));
		buttonPane.add(editCourseButton);
		buttonPane.add(removeCoursesButton);
		
		JScrollPane dispClassesScrollPane = new JScrollPane(dispClassesPane);
		dispClassesScrollPane.setPreferredSize(new Dimension(200, 200));
		
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));
		mainPane.add(semLabel);
		mainPane.add(semDropdown);
		mainPane.add(dispClassesScrollPane);
		mainPane.add(buttonPane);
		return mainPane;
	}
}
